package com.kk.framework.session;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.zkoss.zk.ui.Execution;

public class CookieManager 
{
	public static void setCookie(Execution exe, String cookieName, String cookieValue)
	{
		Cookie cookie = new Cookie(cookieName, cookieValue);
		cookie.setMaxAge(60*60*24*30);//store 30 days
		String cp = exe.getContextPath();
		cookie.setPath(cp);
		((HttpServletResponse)exe.getNativeResponse()).addCookie(cookie);
	}
	
	public static String getCookie(Execution exe, String cookieName) 
	{
		Cookie[] cookies = ((HttpServletRequest)exe.getNativeRequest()).getCookies();
		if(cookies!=null)
		{
			for(int i=0;i<cookies.length;i++)
			{
				if(cookieName.equals(cookies[i].getName()))
				{
					String fs = cookies[i].getValue();
					if (fs != null)
						return fs;
				}
			}
		}
		return "";
	}
}
